from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import View
from json import dumps
from apps.Recommend.views import modelmake
from django.views.generic import View
from apps.Index.models import Users
from apps.Movie.models import Movies
import pandas as pd
from apps.ModelUpdate import modelupdate
from apps.Recommend.views import statisticsmodel
from django.conf import settings


class register(View):

    def get(self, request):
        return render(request, 'register.html')

    def post(self, request):
        password = request.POST.get('user.password')
        password_again = request.POST.get('user.password_again')
        if password_again == password:
            user = Users()
            user.phone = request.POST.get('user.phone')
            user.password = password
            user.sexy = request.POST.get('user.sexy')
            user.birthday = request.POST.get('user.birthday')
            user.email = request.POST.get('user.email')
            user.save()
            return redirect(reverse("Index:login"))
        else:
            return render(request, 'message.html', {'message': '两次密码不一样！'})


class login(View):

    def get(self, request):
        modelupdate()
        if(not settings.MODEL):
            modelmake()
        return render(request, 'login.html')

    def post(self, request):
        phone = request.POST.get('username')
        password = request.POST.get('password')
        try:
            user = Users.objects.get(phone=phone, password=password)
            request.session['phone'] = phone
            data = {'msg': '登录成功', 'success': True}
            return redirect(reverse('Index:main'), request)
        except Users.DoesNotExist:
            data = {'msg': '登录失败', 'success': False}
        return HttpResponse(dumps(data, ensure_ascii=False))


class loginout(View):
    def get(self, request):
        request.session.clear()
        request.session.flush()
        return redirect(reverse("Index:login"))


class main(View):
    def get(self, request):
        return render(request, 'main.html')


class changepassword(View):
    def get(self, request):
        return render(request, 'password_modify.html')

    def post(self, request):
        oldPassword = request.POST.get('oldPassword')
        newPassword = request.POST.get('newPassword')
        newPassword2 = request.POST.get('newPassword2')

        if oldPassword == '':
            return render(request, 'message.html', {'message': '旧密码不正确！'})
        if newPassword == '':
            return render(request, 'message.html', {'message': '请输入新密码!'})
        if newPassword != newPassword2:
            return render(request, 'message.html', {'message': '两次新密码不一样！'})

        username = request.session['phone']
        user = Users.objects.get(phone=username)
        if oldPassword != user.password:
            return render(request, 'message.html', {'message': '旧密码不正确！'})
        user.password = newPassword
        user.save()
        return render(request, 'message.html', {'message': '密码修改成功！'})


class readfile(View):

    def post(self, request):
        try:
            myFile = str(request.FILES.get("myfile", None))
            filepath = r"C:\Users\dell\Desktop\recommend" + '\\' + myFile
            if filepath.endswith('xlsx'):
                data = pd.read_excel(filepath)
            elif filepath.endswith('csv'):
                data = pd.read_csv(filepath)
            print(data)
            Listall = []
            for i in range(len(data)):
                L = list(data.loc[i])[-4:]
                Listall.append(Movies(
                    title=L[0],
                    genres=L[1],
                    movieDesc=L[2],
                    movieYear=L[3]
                ))
            Movies.objects.bulk_create(list(Listall))
            return render(request, 'message.html', {'message': '导入成功！'})
        except:
            return render(request, 'message.html', {'message': '导入失败！'})